var page = {};
//创建显示单个商品信息的页面
function createDisSingleGoodsInfoPage(goodsNo){
    page.curGoodsNo = goodsNo;
    var singGoodsFrm = new Ext.FormPanel({
       renderTo:"singleGoodsInfo",
       width:600,
       height:25,
       title:"",
       labelAlign:"right",
       defaultType:"label",
       items:[
         {text:"商品名称：",id:"goodsNameLabel",name:"goodsNameLabel",style:"font-size:12"},
         {text:"",id:"goodsName",name:"goodsName",style:"font-size:12; margin-right: 10px"},
         {text:"商品价格：",name:"goodsPriceLabel",style:"font-size:12;"},
         {text:"",id:"goodsPrice",name:"goodsPrice",style:"font-size:12;margin-right: 10px"},
         {text:"币种：",name:"goodsPriceUnitLabel",style:"font-size:12"},
         {text:"",id:"goodsPriceUnit",name:"goodsPriceUnit",style:"font-size:12"}
       ]
    });
    fillSigleGoodsFrm(singGoodsFrm,goodsNo);
    findGoodsNos();
    hideMoveBtn();
}
//填充表单
function fillSigleGoodsFrm(singleGoodsFrm,goodsNo){
    $.ajax({
       type:"GET",
       data:"goodsVO.id=" + goodsNo,
       url:"findSingleGoods.action",
       async: false,
       success: function(result){
         var goodsObj = strToJson(result)[0];
         $("#goodsName").html(goodsObj.goodsName);
         $("#goodsPrice").html(goodsObj.goodsPrice);
         $("#goodsPriceUnit").html(goodsObj.goodsPriceUnit);
         $("#singleGoodsPicImg").attr("src",goodsObj.goodsPic);
         $("#singleGoodsPicImg").css("width","480px");
         $("#singleGoodsPicImg").css("height","400px");
         $("#singleGoodsPicImg").parent().css("border","1px solid #ccc");
         //singleGoodsFrm.form.findField("goodsPrice").setValue(goodsObj.goodsPrice);
         //singleGoodsFrm.form.findField("goodsPriceUnit").setValue(goodsObj.goodsPriceUnit);
       }
    });
    bindEvent();
}
//查找所有商品编号
function findGoodsNos(){
   $.ajax({
      type:"GET",
      url:"findGoodsNos.action",
      async: false,
      success: function(result){
        showSystemErrorMsg(result);
        page.goodsNos = strToJson(result);
      }
   });
}
//绑定事件
function bindEvent(){
  $("#disSingleGoodsBackBtn").bind("click",function(){
       returnGoodsPage();
  });
  
  $("#disSingleGoodsBuyBtn").bind("click",function(){
      goBuySingleGoods();
  });
  
  $("#leftMoveBtnImg").bind("mouseover",function(){
     changeMoveBtnWhenMouseOver(this);
  });
  
  $("#leftMoveBtnImg").bind("mouseout",function(){
     changeMoveBtnWhenMouseOut(this);
  });
  
  $("#rightMoveBtnImg").bind("mouseover",function(){
     changeMoveBtnWhenMouseOver(this);
  });
  
  $("#rightMoveBtnImg").bind("mouseout",function(){
     changeMoveBtnWhenMouseOut(this);
  });
  
  $("#rightMoveBtnImg").bind("click",function(){
     disNextImg();
  });
  
  $("#leftMoveBtnImg").bind("click",function(){
    disBeforeImg();
  });
}
//购买单个商品
function goBuySingleGoods(){
   var curUserName = getCurUserName();
   curUserName = curUserName == 'null'? null : curUserName;
   var goodsNo = page.curGoodsNo;
   if(curUserName != "" && curUserName != undefined && curUserName != null){
      load("contentDiv","order.html",false,function(){
           createOrderMenuDiv(goodsNo);
      });
   }else{
      createLoginInterface();
      return;
   }
}
//返回商品显示页面
function returnGoodsPage(){
    load("contentDiv","disGoods.html",false,null);
}
//当鼠标放在移动图片按钮上面的时候，移动图片按钮变宽
function changeMoveBtnWhenMouseOver(curObj){
  $(curObj).css("width","16px");
}
//当鼠标离开移动图片按钮的时候，移动图片按钮恢复原来的宽度
function changeMoveBtnWhenMouseOut(curObj){
  $(curObj).css("width","14px");
}
//显示下一张商品图片
function disNextImg(){
  block();
  page.curIndex = page.curIndex + 1;
  page.curGoodsNo = page.goodsNos[page.curIndex].goodsNo;
  page.curGoodsImg = page.goodsNos[page.curIndex].goodsImg;
  $("#goodsName").html(page.goodsNos[page.curIndex].goodsName);
  $("#goodsPrice").html(page.goodsNos[page.curIndex].goodsPrice);
  $("#goodsPriceUnit").html(page.goodsNos[page.curIndex].priceUnit);
  $("#singleGoodsPicImg").attr("src",page.curGoodsImg);
  hideMoveBtn();
  unblock();
}

//显示上一张商品图片
function disBeforeImg(){
  block();
  page.curIndex = page.curIndex - 1;
  page.curGoodsNo = page.goodsNos[page.curIndex].goodsNo;
  page.curGoodsImg = page.goodsNos[page.curIndex].goodsImg;
  $("#goodsName").html(page.goodsNos[page.curIndex].goodsName);
  $("#goodsPrice").html(page.goodsNos[page.curIndex].goodsPrice);
  $("#goodsPriceUnit").html(page.goodsNos[page.curIndex].priceUnit);
  $("#singleGoodsPicImg").attr("src",page.curGoodsImg);
  hideMoveBtn();
  unblock();
}
//动态隐藏移动按钮
function hideMoveBtn(){
  var index = findCurCurGoodsNoIndex();
  page.curIndex = index;
  var length = page.goodsNos.length;
  if(index == 0 && length == 1){
    //如果只有一张商品图片，左边移动按钮和右边移动按钮都隐藏
    $("#leftMoveBtnImg").hide();
    $("#rightMoveBtnImg").hide();
  }else if(index == 0){
    //如果当前图片是第一张图片，右边移动按钮隐藏起来
    $("#leftMoveBtnImg").hide();
    $("#rightMoveBtnImg").css("display","");
  }else if(index == (length - 1)){
    //如果是最后一张图片，隐藏右边移动按钮
    $("#leftMoveBtnImg").css("display","");
    $("#rightMoveBtnImg").hide();
  }else{
    //如果当前图片既不是第一张图片，也不是最后一张图片，左边移动按钮和右边移动按钮都显示出来
    $("#leftMoveBtnImg").css("display","");
    $("#rightMoveBtnImg").css("display","");
  }
}
//找到当前商品编号在page.goodsNos数组中的索引
function findCurCurGoodsNoIndex(){
   var goodsNo = page.curGoodsNo;
   var index = 0;
   var goodsNos = page.goodsNos;
   if(goodsNos && goodsNos.length > 0){
       var tempGoodsNo = null;
	   for(var i = 0; i < goodsNos.length;i++){
	      tempGoodsNo = goodsNos[i].goodsNo;
	      index = i;
	      if(tempGoodsNo && tempGoodsNo == goodsNo){
	         break;
	      }
	   }
   }
   return index;
}